/*
 * dancingI.cpp
 *
 *  Created on: Sep 18, 2011
 *      Author: jiqinwang
 */

//#include "dancingI.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dancingI.h"
#define PI  3.1415926
//#define tilt 0.253681
#define tilt 0.16667
#define base 0.2

float dancingI::getX(float from_top){
    return (ratio*x*(aux+from_top)/aux);
}

float dancingI::getZ(float from_top){
    return (sin(tilt)*from_top+top_length);
}


dancingI::~dancingI(){
    delete top;
    delete bottom;
    delete left;
    delete right;
    delete front;
    delete back;

}

//constructs the i and calculate the coordinates
dancingI::dancingI(){
       float tu,tl,bu,bl,xu,xl,a,b,h,z1,z2,z3,z4;
       x=base;
       ratio=1.618;
       top_length=x*ratio;
       half_height=x*4.85243;
       bottom_length=top_length*ratio;
       aux=half_height*2/(ratio-1);
       a=tilt;
       tu=x*ratio;
       b=atanf(tu/aux);
       xu=x;
       h=half_height-2*xu;
     //  tl=getX(2*xu);
       tl=getZ(2*xu);

       bu=x*pow(ratio,2.f)+.052;
       xl=xu*(bu/tl);
      // bl=getX(4*xu+2*h);
       bl=getZ(4*xu+2*h);
       z1=x*ratio;
       z2=getZ(2*xu);
       z3=getZ(2*xu+2*h);
       z4=getZ(4*xu+2*h);


       //front upper left corner
       front=new dancingI::face();
       front->insertVertices(-tu,h+2*xu,z1);//0
       front->insertVertices(tu,h+2*xu,z1);//1
       front->insertVertices(tl,h,z2);//2
       front->insertVertices(xu,h,z2);//3
       front->insertVertices(xl,-h,z3);//4
       front->insertVertices(bu,-h,z3);//5
       front->insertVertices(bl,-h-2*xu,z4);//6
       front->insertVertices(-bl,-h-2*xu,z4);//7
       front->insertVertices(-bu,-h,z3);//8
       front->insertVertices(-xl,-h,z3);//9
       front->insertVertices(-xu,h,z2);//10
       front->insertVertices(-tl,h,z2);//11
       front->norm=(front->vertices[0]-front->vertices[1]).cross(front->vertices[0]-front->vertices[11]);
       //lower left counter clockwise
       top=new dancingI::face();
       top->insertVertices(-tu,h+2*xu,z1);
       top->insertVertices(tu,h+2*xu,z1);
       top->insertVertices(tu,h+2*xu,-z1);
       top->insertVertices(-tu,h+2*xu,-z1);
       top->norm=Vec3f(0, 1, 0);
       
       //lower left counter clockwise
       bottom=new dancingI::face();
       bottom->insertVertices(bl,-h-2*xu,bl);
       bottom->insertVertices(-bl,-h-2*xu,bl);
       bottom->insertVertices(-bl,-h-2*xu,-bl);
       bottom->insertVertices(bl,-h-2*xu,-bl); 
       bottom->norm=Vec3f(0, -1, 0);
       
       left=new dancingI::face();
       for(unsigned int i=0;i<front->vertices.size();i++){
           left->insertVertices(front->vertices[i].v[2],front->vertices[i].v[1],front->vertices[i].v[0]);
       }
       left->norm=(left->vertices[0]-left->vertices[1]).cross(left->vertices[0]-left->vertices[11]);
       
       back=new dancingI::face();
       for(unsigned int i=0;i<front->vertices.size();i++){
           back->insertVertices(-front->vertices[i].v[0],front->vertices[i].v[1],-front->vertices[i].v[2]);
       }
              back->norm=(back->vertices[0]-back->vertices[1]).cross(back->vertices[0]-back->vertices[11]);

       right=new dancingI::face();
       for(unsigned int i=0;i<left->vertices.size();i++){
           right->insertVertices(-left->vertices[i].v[0],left->vertices[i].v[1],left->vertices[i].v[2]);
       }
              right->norm=(right->vertices[0]-right->vertices[1]).cross(right->vertices[0]-right->vertices[11]);

       
}



int dancingI::face::insertVertices(float t[3]){
    vertices.push_back(Vec3f(t[0],t[1],t[2]));
    return vertices.size();
}

int dancingI::face::insertVertices(float x,float y, float z){
    vertices.push_back(Vec3f(x,y,z));
    return vertices.size();
}

dancingI::face::face() {
	// TODO Auto-generated constructor stub
}

dancingI::face::~face() {
	// TODO Auto-generated destructor stub
}
